package com.jiangyg.mall.coupon.controller;

import com.jiangyg.mall.core.support.restful.Pagination;
import com.jiangyg.mall.core.support.restful.Result;
import com.jiangyg.mall.coupon.dto.HomeSubjectSpuDTO;
import com.jiangyg.mall.coupon.service.HomeSubjectSpuService;
import com.jiangyg.mall.coupon.vo.HomeSubjectSpuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 类描述：专题商品控制器
 *
 * @author jiangyg
 * @version email@jiangyg.com
 * @date 2021-10-18 21:33:05
 */
@RestController
@RequestMapping("/coupon/home/subject/spu")
public class HomeSubjectSpuController {

    /**
     * 专题商品接口
     */
    private final HomeSubjectSpuService homeSubjectSpuService;

    @Autowired
    public HomeSubjectSpuController(HomeSubjectSpuService homeSubjectSpuService) {
        this.homeSubjectSpuService = homeSubjectSpuService;
    }

    /**
     * 功能描述：专题商品分页查询
     *
     * @param request    请求
     * @param pagination 分页参数
     * @return 返回分页专题商品
     */
    @ResponseBody
    @PostMapping("/select")
    public Result<Pagination<HomeSubjectSpuVO>> select(HttpServletRequest request, @RequestBody Pagination<HomeSubjectSpuVO> pagination) throws Exception {
        return Result.ok(homeSubjectSpuService.selectPagination(pagination));
    }

    /**
     * 功能描述：保存或者修改专题商品
     *
     * @param request 请求
     * @param dto     专题商品
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/save")
    public Result<Object> save(HttpServletRequest request, @RequestBody @Validated HomeSubjectSpuDTO dto) throws Exception {
        return Result.saveOrUpdate(homeSubjectSpuService.saveOrUpdate(dto));
    }

    /**
     * 功能描述：根据ID获取专题商品
     *
     * @param request 请求
     * @param homeSubjectSpuId  专题商品ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/info")
    public Result<HomeSubjectSpuVO> info(HttpServletRequest request, @RequestParam(name = "homeSubjectSpuId") Long homeSubjectSpuId) throws Exception {
        return Result.ok(homeSubjectSpuService.selectById(homeSubjectSpuId));
    }

    /**
     * 功能描述：删除专题商品
     *
     * @param request 请求
     * @param homeSubjectSpuIds 批量删除的专题商品ID
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/delete")
    public Result<Object> delete(HttpServletRequest request, @RequestBody List<Long> homeSubjectSpuIds) {
        return Result.delete(homeSubjectSpuService.delete(homeSubjectSpuIds));
    }

}
